package com.lottery.im.module.service.ui;

import android.annotation.SuppressLint;
import android.widget.ListView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.ServiceData;
import com.lottery.im.api.result.entity.UserInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.main.ui.AgentWebViewActivity;
import com.lottery.im.module.service.adapter.ServiceAdapter;
import com.lottery.im.module.service.presenter.ServicePresenter;
import com.lottery.im.module.service.view.ServiceView;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;
import com.lottery.im.view.DancingView;
import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment") public class ServiceFragment extends BaseSwipeFragment<ServicePresenter> implements ServiceView {

  @BindView(R.id.tv_point) DancingView mTvPoint;
  @BindView(R.id.list) ListView mList;

  private ServiceAdapter mAdapter;
  private List<ServiceData> serviceDataList;
  private double amount;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_service;
  }

  @Override protected void initPresenter() {
    presenter = new ServicePresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    serviceDataList = new ArrayList<>();
    ServiceData serviceData = new ServiceData();
    serviceData.setTitle("活动中心");
    List<ServiceData.ReturnListBean> listBeans = new ArrayList<>();
    listBeans.add(new ServiceData.ReturnListBean("", 0, "",
        "签到", 0, "", ""));
    listBeans.add(new ServiceData.ReturnListBean("", 0, "",
        "整点红包", 0, "", ""));
    listBeans.add(new ServiceData.ReturnListBean("", 0, "",
        "即将开启", 0, "", ""));
    serviceData.setReturnList(listBeans);
    serviceDataList.add(serviceData);
    mAdapter = new ServiceAdapter(getActivity(), serviceDataList);
    mList.setAdapter(mAdapter);
    mAdapter.setOnServiceClickListener((position, childPosition) -> {
      if (position == 0 && childPosition == 0) {
        startFragment(new SigninFragment());
      } else if (position == 0 && childPosition == 1) {
        startFragment(new Type3SQListFragment());
      } else if (position == 1) {
        AgentWebViewActivity.create(this,
            serviceDataList.get(position).getReturnList().get(childPosition).getUrl(),
            serviceDataList.get(position).getReturnList().get(childPosition).getName());
      }
    });

    presenter.loadServiceData();
    presenter.queryUserCommonInfo();

  }

  @OnClick(R.id.btn_point) public void onViewClicked() {
    // 点击查看
    startFragment(new IntegralFragment(amount));
  }

  @Override protected boolean canDragBack() {
    return false;
  }

  @Override public void loadSuccess(List<ServiceData.ReturnListBean> list) {
    ServiceData serviceData = new ServiceData();
    serviceData.setTitle("第三方服务");
    serviceData.setReturnList(list);
    serviceDataList.add(serviceData);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void queryUserCommonInfo(UserInfo userCommon) {
    mTvPoint.setText(String.valueOf(amount = userCommon.getAmount()));
    mTvPoint.setDuration(amount);
    mTvPoint.dance();
  }

  @Override public void onStart() {
    super.onStart();
    RxBus.get().register(this);
  }

  @Override public void onStop() {
    super.onStop();
    RxBus.get().unRegister(this);
  }

  @Subscribe(code = RxBusCode.ONRESUME_CODE)
  public void refreshOnResume(){
    presenter.queryUserCommonInfo();
  }
}
